package com.pz.memberunit.controller;


import com.pz.art.domain.AssoMember;
import com.pz.art.service.MemberService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/unit")
public class UnitMemberController extends BaseController {

    @Autowired
    private MemberService memberService;


    /**
     * 查询所有会员列表
     */
    @PreAuthorize("@ss.hasPermi('unit:memberlist')")
    @GetMapping("/memberlist")
    public TableDataInfo list() {
        startPage();
        List<AssoMember> list = memberService.getAssomemberByUnitId(getUserId());
        return getDataTable(list);
    }

    @Log(title = "普通会员管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('unit:normalexport')")
    @PostMapping("/norunitmalexport")
    public void normalexport(HttpServletResponse response, AssoMember memberUnit) {
        try {
            List<AssoMember> list = memberService.getAssomemberByUnitId(getUserId());
            ExcelUtil<AssoMember> util = new ExcelUtil<AssoMember>(AssoMember.class);
            util.exportExcel(response, list, "单位会员数据");
        } catch (Throwable e) {
            System.out.println(e.getMessage());
        }

    }

    /**
     * 获取会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('unit:unitmember')")
    @GetMapping(value = "/unitmemberdetail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(memberService.selectAssoMemberById(id));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('unit:unitmemberremove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/unitmemberremove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(memberService.deleteAssoMemberByIds(ids));
    }

    /**
     * 查询所有普通会员列表
     */
    @PreAuthorize("@ss.hasPermi('unit:normalmemberlist')")
    @GetMapping("/normalmemberlist")
    public TableDataInfo getmembernormalList() {
        startPage();
        List<AssoMember> list = memberService.selectNormalAllMember(new AssoMember());
        return getDataTable(list);
    }

    /**
     * 传入会员id进行更新会员所属单位
     */
    @PreAuthorize("@ss.hasPermi('unit:memberlist')")
    @GetMapping("/updatememberunit/{ids}")
    public AjaxResult updateMemberUnit(@PathVariable Long[] ids) {
        Integer Result = memberService.updateAssoMemberUnit(ids, getUserId());
        return success(Result);
    }
}
